package com.lucy.disk.domain.entity;

import cn.hutool.core.io.file.FileNameUtil;
import com.lucy.common.converter.JpaColumnConverter;
import com.lucy.common.core.BaseEntity;
import com.lucy.disk.constant.DiskConst;
import com.lucy.disk.converter.FileExtendConverter;
import com.lucy.disk.domain.model.BaseFile;
import com.lucy.disk.utils.DiskUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.hibernate.Hibernate;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Objects;

/**
 * @author kindear
 */
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "disk_file")
@ApiModel(value = "文件实体(含文件夹)",description = "")
public class FileEntity extends BaseEntity implements Serializable {

    public FileEntity(String fileName){
        setFileName(fileName);
        String extName = FileNameUtil.extName(fileName);

        setExtName(extName);
        // 设置文件状态 正常
        setFileStatus(1);
        // 设置文件只读 false
        setReadOnly(0);
        // 设置存储桶名称
        setBucketName(DiskConst.DEFAULT_OSS_BUCKET_NAME);
        // 设置文件类型
        String fileType = DiskUtil.getFileType(fileName);
        setFileType(fileType);
    }

    @ApiModelProperty("存储桶名称")
    @Column(name = "bucket_name")
    private String bucketName;


    @ApiModelProperty(value = "OSS地址")
    @Column(name = "oss_url")
    private String ossUrl;

    @ApiModelProperty(value = "目录ID")
    @Column(name = "dir_id")
    private String dirId;

    @ApiModelProperty(value = "用户ID")
    @Column(name = "user_id")
    String userId;

    @ApiModelProperty(value = "是否为目录(文件夹)")
    @Column(name = "is_dir")
    private Integer isDir;

    @ApiModelProperty(value = "文件名")
    @Column(name = "file_name")
    private String fileName;

    @ApiModelProperty(value = "拓展名")
    @Column(name = "ext_name")
    private String extName;

    @ApiModelProperty(value = "文件大小")
    @Column(name = "file_size")
    private Long fileSize;

    @ApiModelProperty(value = "文件状态",notes = "0 为禁止访问 1 为正常")
    @Column(name = "file_status")
    private Integer fileStatus;

    /**
     * 如何理解文件类型 文档 / 图片 / 视频 / 回收站
     */
    @ApiModelProperty(value = "文件类型",notes = "")
    @Column(name = "file_type")
    private String fileType;

    @ApiModelProperty(value = "唯一识别编码")
    @Column(name = "md5")
    private String md5;

    @ApiModelProperty(value = "只读")
    @Column(name = "read_only")
    private Integer readOnly;


//    @Convert(converter = FileExtendConverter.class)
//    @ApiModelProperty(value = "基础文件信息")
//    @Column(name = "file_extend")
//    private BaseFile fileExtend;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        FileEntity that = (FileEntity) o;
        return getId() != null && Objects.equals(getId(), that.getId());
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
